package com.prothink.threadproject.controller;

import cloud.tianai.captcha.common.response.ApiResponse;
import com.prothink.threadproject.common.Result;
import com.prothink.threadproject.service.UserService;
import com.prothink.threadproject.vo.PasswordRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/api")
public class RegistrationController {

    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    private UserService userService;

    private Map<String, String> verificationCodes = new HashMap<>();
    // 修改用户密码
//    @PostMapping("/updatePassword")
//    public Result<?> updatePassword(@RequestBody PasswordRequest passwordRequest) {
//        // 从请求中获取 id 和 newPassword
//        Long id = passwordRequest.getId();
//        String newPassword = passwordRequest.getNewPassword();
//
//        // 验证密码强度
//        String passwordStrength = evaluatePasswordStrength(newPassword);
//        if ("低".equals(passwordStrength)) {
//            return Result.failure("密码强度过低，请使用数字+字母+特殊字符的组合");
//        }
//
//        // 进行密码更新操作
//        int result = userService.updateUserPassword(id, newPassword);
//        if (result > 0) {
//            return Result.success("密码修改成功");
//        } else {
//            return Result.failure("密码修改失败");
//        }
//    }



    // 内部方法：评估密码强度
    @PostMapping("/evaluatePasswordStrength")
    public String evaluatePasswordStrength(@RequestBody Map<String, String> requestBody) {
        String password = requestBody.get("password");

        System.out.println("收到的 password: " + password); // 打印接收到的密码

        if (password.matches("^\\d+$")) {
            return "低"; // 纯数字
        } else if (password.matches("^(?=.*[a-zA-Z])(?=.*\\d)[a-zA-Z\\d]+$")) {
            return "中等"; // 字母+数字
        } else if (password.matches("^(?=.*[a-zA-Z])(?=.*\\d)(?=.*[\\W_])[a-zA-Z\\d\\W_]+$")) {
            return "高"; // 字母+数字+特殊字符
        }
        return "低"; // 默认返回低强度
    }





    @PostMapping("/sendVerificationCode")
    public String sendVerificationCode(@RequestBody Map<String, String> request) {
        String email = request.get("email");
        String code = String.format("%06d", new Random().nextInt(999999)); // 保证6位验证码

        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(email);
        message.setSubject("注册验证码");
        message.setText("您的验证码是: " + code);

        mailSender.send(message);
        verificationCodes.put(email, code);

        return "验证码已发送";
    }


    @PostMapping("/register")
    public String register(@RequestBody Map<String, String> request) {
        String email = request.get("email");
        String code = request.get("verificationCode");

        if (code.equals(verificationCodes.get(email))) {
            // 验证码正确，执行注册逻辑
            // TODO: 保存用户到数据库
            verificationCodes.remove(email); // 清除已使用的验证码
            return "注册成功";
        } else {
            return "验证码错误";
        }
    }
}
